import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/form_switch_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';

///新建班级列表
class NewClassViewModel extends ChangeNotifier {
  BuildContext me;
  TextEditingController schoolName = TextEditingController();
  TextEditingController gradeName = TextEditingController();
  TextEditingController className = TextEditingController();

  String kemu = "";

  List<FormSwitchModel> gridList = [];

  NewClassViewModel(this.me);

  initView() {
    DioUtils.getInstance()
        .post(ApiService.getSubjects, {"token": Constant.getToken()},
            (successCallBack) {
      gridList.clear();
      (successCallBack["data"]["subject_list"] as List).forEach((element) {
        DioUtils.getInstance()
            .post(ApiService.getSubjectIcon, {"id": element["id"]},
                (successCallBack1) {
          if (successCallBack1["data"] == null) {
            return;
          }
          gridList.add(FormSwitchModel(element["name"],
              imageUrl: successCallBack1["data"]["subject_icon"]["icon"] ?? "",
              imageUrlFalse:
              successCallBack1["data"]["subject_icon"]["false_icon"] ?? "",
              judge: false,
              fileAndUrl: 1));
        }, (errorCallBack) {});
      });
      notifyListeners();
    }, (errorCallBack) {});
  }

  newOnTap() {
    if (schoolName.text == "") {
      return;
    }

    if (gradeName.text == "") {
      return;
    }

    if (className.text == "") {
      return;
    }
//    token	文本	必填	令牌
//    school	文本	必填	学校
//    grade	文本	必填	年级（1-12：小学-高中）
//    subject	文本	必填	科目
//    cls	文本	必填	班级名称
    DioUtils.getInstance().post(ApiService.createCls, {
      "token": Constant.getToken(),
      "school": schoolName.text,
      "grade": gradeName.text,
      //科目名字
      "subject": kemu,
      "cls": className.text,
    }, (successCallBack) {
      Fluttertoast.showToast(msg: "添加成功");
      Navigator.pop(me);
      notifyListeners();
    }, (errorCallBack) {});
  }

  onTapList(int index) {
    for (int i = 0; i < gridList.length; i++) {
      gridList[i].judge = i == index;
    }
    kemu = gridList[index].title;
    notifyListeners();
  }
}
